﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using qlbansach_thuctapnhom.DAL;
using qlbansach_thuctapnhom.BLL;
namespace qlbansach_thuctapnhom.form
{
    public partial class Form_loaisach : Form
    {
        public Form_loaisach()
        {
            InitializeComponent();
            hienthi();
        }
        quanlybansachDataContext db = new quanlybansachDataContext();
        loaisach_bll ls = new loaisach_bll();
        
        //=======================================================
        public void hienthi()
        {
            db = new quanlybansachDataContext();
            var hienthi = from ht in db.loaisaches select ht;
            dgv.DataSource = hienthi;
            dgv.DataBindings.Clear();
        }
        //=======================================================
        private void btthem_Click(object sender, EventArgs e)
        {
            loaisach obj = new loaisach();
            bool ok = ls.kiemtraloai(txtmaloai.Text);
            if (ok)
            {

                obj.maloai = txtmaloai.Text;
                obj.ten = txttenloai.Text;
               ls.themloai(obj);
                lblerror.Text = "Thêm thành công";

            }
            else MessageBox.Show("Thêm thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            hienthi();

        }
        //=======================================================
        private void btnhaplai_Click(object sender, EventArgs e)
        {

            foreach (var c in groupPanel1.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
            }
        }
        //=======================================================
        private void btthoat_Click(object sender, EventArgs e)
        {
            formchinh f = new formchinh();
            f.Show();
            this.Close();
        }

        private void btsua_Click(object sender, EventArgs e)
        {
            bool ok = ls.kiemtraloai(txtmaloai.Text);
            if (!ok)
            {
                ls.sua(txtmaloai.Text, txttenloai.Text);
            
                lblerror.Text = "Sửa thành công";
            }
            else MessageBox.Show("Sửa thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            hienthi();
        }

        private void btxoa_Click(object sender, EventArgs e)
        {
            bool ok = ls.kiemtraloai(txtmaloai.Text);
            if (!ok)
            {
                ls.xoa(txtmaloai.Text);
             
                lblerror.Text = "Xóa thành công";


            }
            else MessageBox.Show("Không tìm thấy đối tượng cần xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            hienthi();
        }

        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int n = e.RowIndex;//n is the location of the selected line            
            txtmaloai.Text = dgv.Rows[n].Cells[0].Value.ToString();
            txttenloai.Text = dgv.Rows[n].Cells[1].Value.ToString();
        }

        private void bttimaloai_Click(object sender, EventArgs e)
        {
            bool ok = ls.timkiemmaloai(txtmaloai.Text);
            if (ok)
            {
                dgv.DataSource = from l in db.loaisaches where l.maloai.Equals(txtmaloai.Text) select l;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
     
        }

        private void bttimtenloai_Click(object sender, EventArgs e)
        {
            bool ok = ls.timkietenloai(txtmaloai.Text);
            if (ok)
            {
                dgv.DataSource = from l in db.loaisaches where l.ten.Equals(txttenloai.Text) select l;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
     
        }
    }
}
